package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.ConstructionSchedule;
import com.hengxinguotong.zhihuichengjian.treeview.*;
import com.hengxinguotong.zhihuichengjian.treeview.RecyclerAdapter;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/2/26.
 */

public class ConstructionScheduleAdapter extends RecyclerAdapter<ConstructionSchedule, ConstructionScheduleAdapter.ScheduleViewHolder> {

    public ConstructionScheduleAdapter(Context context, List<ConstructionSchedule> dataList) {
        super(context, dataList);
    }

    @Override
    public ScheduleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_construction_schedule, null);
        return new ScheduleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ScheduleViewHolder holder, int position) {
        holder.bindData(position);
    }

    public class ScheduleViewHolder extends RecyclerAdapter.BaseViewHolder<ConstructionSchedule>{
        @BindView(R.id.name_tv)
        HXTextView nameTv;
        @BindView(R.id.days_tv)
        HXTextView daysTv;
        @BindView(R.id.percent_tv)
        HXTextView percentTv;
        @BindView(R.id.construction_progress)
        ProgressBar constructionProgress;
        @BindView(R.id.start_date)
        HXTextView startDate;
        @BindView(R.id.end_date)
        HXTextView endDate;

        public ScheduleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void bindData(final int position) {
            ConstructionSchedule constructionSchedule = dataList.get(position);
            nameTv.setText(constructionSchedule.getName());
            daysTv.setText("剩余" + constructionSchedule.getDaysRemain() + "天");
            Float percent = Float.parseFloat(constructionSchedule.getPercent());
            DecimalFormat df = new DecimalFormat("#.##");
            String percentStr = df.format(percent * 100);
            percentTv.setText(percentStr + "%");
            int progress = (int) (percent * 100);
            constructionProgress.setProgress(progress);
            startDate.setText(constructionSchedule.getStartDate());
            endDate.setText(constructionSchedule.getEndDate());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position);
                }
            });
        }
    }
}
